package com.qiu.wxapi.controller;

import com.qiu.wxapi.commom.ResponseResult;
import com.qiu.wxapi.mapper.NoticeMapper;
import com.qiu.wxapi.pojo.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("notice")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 发布公告
     * @param notice
     * @return
     */
    @PostMapping("save")
    public ResponseResult save(@RequestBody Notice notice){
        if (notice==null){
            return ResponseResult.fail("不能为空");
        }
        notice.setCreated(new Date());
        int insert = this.noticeMapper.insert(notice);

        return ResponseResult.success(insert);
    }

    /**
     * 修改公告
     * @param notice
     * @return
     */
    @PostMapping("update")
    public ResponseResult update(@RequestBody Notice notice){
        if (notice==null){
            return ResponseResult.fail("不能为空");
        }
        int insert = this.noticeMapper.insert(notice);
        return ResponseResult.success(insert);
    }
    /**
     * 查询公告
     */
    @RequestMapping
    public ResponseResult find(){
        Example example = new Example(Notice.class);
        example.setOrderByClause("created  DESC");
        List<Notice> notices = this.noticeMapper.selectByExample(example);
        return ResponseResult.success(notices);
    }

    /**
     * 删除公告
     * @param notice
     * @return
     */
    @PostMapping("delete")
    public ResponseResult delete(@RequestBody Notice notice){
        if (notice==null){
            return ResponseResult.fail("不能为空");
        }
        int delete = this.noticeMapper.delete(notice);
        return ResponseResult.success(delete);
    }

}
